<?php

class CF_PdoActs{

	
	var $is_good;
	var $statusmsg;
	
	
	// Only adds one record...
	function process_I_aftermax(){
		$newkeyindex = 0;
        foreach($this->fieldspec AS $col_name => $colspec){
		// for input values, accept only non empty values.
            if(!empty($this->newvalues[0][$col_name]) OR $this->newvalues[0][$col_name] === '0'){
				$thisval = $this->newvalues[0][$col_name];
				//$thisval = addslashes($thisval);
				($vars=='')?$vars=$col_name:$vars.=",$col_name";
				//($vals=='')?$vals = "'" . $thisval . "'":$vals .= ",'" . $thisval . "'";
				($vals=='')?$vals = ":" . $col_name . " ":$vals .= ", :" . $col_name . " ";
				}
				// If we no data sent for this variable and it is a key, get new value
			else if(!empty($colspec['key'])){
				($vars=='')?$vars=$col_name:$vars.=",$col_name";
				//($vals=='')?$vals = $this->get_next_auto_id($col_name):$vals .= "," . $this->get_next_auto_id($col_name);
				($vals=='')?$vals = ":" . $col_name . " ":$vals .= ", :" . $col_name . " ";
				$newkeys[$newkeyindex] ['var'] = $col_name;
				$newkeys[$newkeyindex] ['val'] = $this->get_next_auto_id($col_name);
				$newkeyindex ++;
				}
            }
        if($vars != ''){
            $qry_insert = "INSERT INTO $this->maintable ($vars) VALUES ($vals)";
			//print $qry_insert;
		global $dbh;
		$stmt = $dbh->prepare($qry_insert);	
        foreach($this->fieldspec AS $col_name => $colspec){
		// for input values, accept only non empty values.
	            if(!empty($this->newvalues[0][$col_name]) OR $this->newvalues[0][$col_name] === '0'){
				$thisval = $this->newvalues[0][$col_name];
				$bparam = ':' . $col_name; // NEED to escape this colname to take out any non-normal characters
				$stmt->bindValue($bparam ,$thisval);
				//print "Binding $thisval TO $bparam  ";
			}
		}	
		foreach($newkeys AS $newkeyidx => $newkey){
			$bparam = ':' . $newkey['var']; // NEED to escape this colname to take out any non-normal characters
			$thisval = $newkey['val'];
			$stmt->bindValue($bparam ,$thisval);
			
		}
		
			try{
			    /*** fetch into an PDOStatement object ***/
				//$stmt = $dbh->prepare($qry_insert);

				/*** echo number of columns ***/
				$stmt->execute();
				$result =  $stmt->rowCount();
				}
			catch(PDOException $e){	
				$this->is_good = false;
				$this->statusmsg = $e->getMessage();
				}
            }	
	}	

	
	// Only adds one record...
	function process_I_autoinc(){
        foreach($this->fieldspec AS $col_name => $colspec){
		// for input values, accept only non empty values.
            if(!empty($this->newvalues[0][$col_name]) OR $this->newvalues[0][$col_name] === '0'){
				$thisval = $this->newvalues[0][$col_name];
				//$thisval = addslashes($thisval);
				($vars=='')?$vars=$col_name:$vars.=",$col_name";
				//($vals=='')?$vals = "'" . $thisval . "'":$vals .= ",'" . $thisval . "'";
				($vals=='')?$vals = ":" . $col_name . " ":$vals .= ", :" . $col_name . " ";
				}
				// If we no data sent for this variable and it is a key, Dont include it, and let autoincrement catch it
			else if(!empty($colspec['key'])){
				$thekeyvar = $col_name;
            }
		}
        if($vars != ''){
            $qry_insert = "INSERT INTO $this->maintable ($vars) VALUES ($vals)";
			//print $qry_insert;
		
		global $dbh;
		$stmt = $dbh->prepare($qry_insert);	
        foreach($this->fieldspec AS $col_name => $colspec){
		// for input values, accept only non empty values.
	            if(!empty($this->newvalues[0][$col_name]) OR $this->newvalues[0][$col_name] === '0'){
				$thisval = $this->newvalues[0][$col_name];
				$bparam = ':' . $col_name; // NEED to escape this colname to take out any non-normal characters
				$stmt->bindValue($bparam ,$thisval);
				//print "Binding $thisval TO $bparam  ";
			}
		}			
		//	$stmt->debugDumpParams();
			
			try{
			    /*** fetch into an PDOStatement object ***/
				//$stmt = $dbh->prepare($qry_insert);

				/*** echo number of columns ***/
				$stmt->execute();
				$result =  $stmt->rowCount();
				}
			catch(PDOException $e){	
				$this->is_good = false;
				$this->statusmsg = $e->getMessage();
				}
			try{
				if(!empty($this->sequence)){
				$thekeyval = $dbh->lastInsertID($this->sequence);
				}else{
				$thekeyval = $dbh->lastInsertID();
				}
				$this->keyvalues[$thekeyvar] = $thekeyvalue;
			}
			catch(PDOException $e){	
				$this->is_good = false;
				$this->statusmsg = $e->getMessage();
								
			}
		}	
	}

	

function process_Save(){
	global $dbh;
	if(is_array($this->keyvalues)){
	foreach($this->keyvalues AS $keyindex => $keys){
	// prepare where string from passed keys
		$afterand = '';
		$aftercomma = '';		
		foreach($keys AS $col_name => $col_value){
			//print "$col_name = $col_value <br />";
			$wherestr .= $afterand . " $col_name=:KY$col_name ";
			$afterand = ' AND ';
		}

		if(is_array($this->newvalues[$keyindex])){
		foreach($this->newvalues[$keyindex] AS $val_name => $val_value){
			$setstr .= $aftercomma . " $val_name=:ST$val_name  ";
			$aftercomma = ',';
		}
		}		


		// Create prepared statement
		$qry = "UPDATE $this->maintable SET $setstr  WHERE ( " . $wherestr . " )";
		//print $qry;
	$sth = $dbh->prepare($qry);	
	// bind variables to passed keys	
		foreach($keys AS $col_name => $col_value){
			$bparam = ':KY' . $col_name; // NEED to escape this colname to take out any non-normal characters
			$sth->bindValue($bparam ,$col_value);
			//print "Binding $bparam TO $col_value";
		}
	// bind variables to passed set stmt
		if(is_array($this->newvalues[$keyindex])){	
		foreach($this->newvalues[$keyindex] AS $val_name => $val_value){
			
			$bparam = ':ST' . $val_name; // NEED to escape this colname to take out any non-normal characters
			$sth->bindValue($bparam ,$val_value);
			//print "Biding $bparam TO $val_value ";
		}
		}		
		try{
	    /*** fetch into an PDOStatement object ***/
		//$stmt = $dbh->prepare($qry);

//$sth->debugDumpParams();
		/*** echo number of columns ***/
		$sth->execute();
		$result =  $sth->rowCount();
		}
		catch(PDOException $e){	
			$this->is_good = false;
			$this->statusmsg = $e->getMessage();
			print $this->statusmsg;
			
		}
		$setstr = '';
		$wherestr = '';		
	}
	}
}
	
function process_OldSave(){
	if(is_array($this->keyvalues)){
	foreach($this->keyvalues AS $keyindex => $keys){
		//print "<br />";
		$afterand = '';
		foreach($keys AS $col_name => $col_value){
			//print "$col_name = $col_value <br />";
			$wherestr .= $afterand . " $col_name = '$col_value' ";
			$afterand = ' AND ';
		}
		$aftercomma = '';
		if(is_array($this->newvalues[$keyindex])){
		foreach($this->newvalues[$keyindex] AS $val_name => $val_value){
			$setstr .= $aftercomma . " $val_name = '$val_value' ";
			$aftercomma = ',';
		}
		}
	$qry =  "UPDATE $this->maintable SET $setstr WHERE 1=0 OR ( " . $wherestr . " )";

	global $dbh;
	try{
	    /*** fetch into an PDOStatement object ***/
		$stmt = $dbh->prepare($qry);

		/*** echo number of columns ***/
		$stmt->execute();
		$result =  $stmt->rowCount();
	}
	catch(PDOException $e){	
		$this->is_good = false;
		$this->statusmsg = $e->getMessage();
	}
	$setstr = '';
	$wherestr = '';	
	}
	}
}
	

function process_Delete(){
	foreach($this->keyvalues AS $keyindex => $keys){
		//print "<br />";
		$afercomma = '';
		$afterand = '';
		foreach($keys AS $col_name => $col_value){
			//print "$col_name = $col_value <br />";
			$wherestr .= $afterand . " $col_name=:KY$col_name ";
			$afterand = ' AND ';
		}

	$qry = "DELETE FROM $this->maintable WHERE 1=0 OR ( " . $wherestr . " )";
	global $dbh;
	$stmt = $dbh->prepare($qry);	
	// bind variables to passed keys	
		foreach($keys AS $col_name => $col_value){
			$bparam = ':KY' . $col_name; // NEED to escape this colname to take out any non-normal characters
			$stmt->bindValue($bparam ,$col_value);
			//print "Binding $bparam TO $col_value";
		}	
	//	$stmt->debugDumpParams();
	try{
	    /*** fetch into an PDOStatement object ***/
		//$stmt = $dbh->prepare($qry);

		/*** echo number of columns ***/
		$stmt->execute();
		$result =  $stmt->rowCount();
	}
	catch(PDOException $e){	
		$this->is_good = false;
		$this->statusmsg = $e->getMessage();
	}
	$wherestr = '';
	}
}	
	
}

?>